package com.mobilesales.android.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

import com.mobilesales.android.model.employees.Employee;
import com.mobilesales.android.model.employees.Message;

public class EmployeeManager extends ManagerBase{

	public EmployeeManager(Context c)
	{
		super(c);
	}
	
	public List<Employee> GetEmployeeList()
	{
     	try {
     		
     		String query = "SELECT _id"
     						+ " FROM Employees";
     		Cursor c = db.rawQuery(query, null);
     		
     		List<Employee> result = new ArrayList<Employee>();
     		if (!c.moveToFirst())
     			return result;
     		
     		List<Integer> tmp = new ArrayList<Integer>();
     		while (!c.isAfterLast()){
     			tmp.add(c.getInt(c.getColumnIndex("_id")));
     			c.moveToNext();
     		}
     		c.close();
     		for (int id : tmp)
     			result.add(GetEmployee(id));
     		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public Employee GetEmployee(int employeeId)
	{
     	try {		
     		
     		String query = "SELECT _id, Firstname, Lastname, Login" 
							+ " FROM Employees" 
							+ " WHERE _id = " + employeeId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Employee result = new Employee();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.Firstname= c.getString(c.getColumnIndex("Firstname"));
     		result.Lastname = c.getString(c.getColumnIndex("Lastname"));
     		result.Login = c.getString(c.getColumnIndex("Login"));
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public boolean StoreEmployee(Employee employee)
	{
     	try {
     		int result;
			ContentValues cv = new ContentValues();

			cv.put("_id", employee.Id);
			cv.put("Firstname",employee.Firstname);
			cv.put("Lastname", employee.Lastname);
			cv.put("Login", employee.Login);
			
			result = db.update("Employees", cv, "_id ="+employee.Id, null);
			if (result == 0)
				result = (db.insert("Employees", null, cv) > 0 ? 1 : 0);
			
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public boolean RemoveEmployee(int employeeId)
	{
     	try {
     		int result;
			result = (int)db.delete("Employees", "_id ="+employeeId, null);
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public List<Message> GetMessageList()
	{
     	try {
     		
     		String query = "SELECT _id"
     						+ " FROM Messages";
     		Cursor c = db.rawQuery(query, null);
     		
     		List<Message> result = new ArrayList<Message>();
     		if (!c.moveToFirst())
     			return result;
     		
     		List<Integer> tmp = new ArrayList<Integer>();
     		while (!c.isAfterLast()){
     			tmp.add(c.getInt(c.getColumnIndex("_id")));
     			c.moveToNext();
     		}
     		c.close();
     		for (int id : tmp)
     			result.add(GetMessage(id));
     				
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public Message GetMessage(int messageId)
	{
     	try {	
     		
     		String query = "SELECT _id, ReceiverId, SenderId, Subject, Content, SendDate, ReceiveDate, IsNew" 
							+ " FROM Messages" 
							+ " WHERE _id = " + messageId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Message result = new Message();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.RecepientId= c.getInt(c.getColumnIndex("ReceiverId"));
     		result.SenderId = c.getInt(c.getColumnIndex("SenderId"));
     		result.Subject = c.getString(c.getColumnIndex("Subject"));
     		result.Content = c.getString(c.getColumnIndex("Content"));
     		result.SendDate = c.getString(c.getColumnIndex("SendDate"));
     		result.ReceiveDate = c.getString(c.getColumnIndex("ReceiveDate"));
     		result.IsNew = c.getInt(c.getColumnIndex("IsNew")) == 1 ? true : false;
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	public boolean StoreMessage(Message message)
	{
     	try {
     		int result;
			ContentValues cv = new ContentValues();

			cv.put("_id", message.Id);
			cv.put("ReceiverId",message.RecepientId);
			cv.put("SenderId", message.SenderId);
			cv.put("Subject", message.Subject);
			cv.put("Content", message.Content);
			cv.put("SendDate", message.SendDate);
			cv.put("ReceiveDate", message.ReceiveDate);
			cv.put("IsNew", message.IsNew);
			
			result = db.update("Messages", cv, "_id ="+message.Id, null);
			if (result == 0)
				result = (db.insert("Messages", null, cv) > 0) ? 1 : 0;
			
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public boolean RemoveMessage(int messageId)
	{
     	try {
     		int result;
			result = db.delete("Messages", "_id ="+messageId, null);
			
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
}
